import { useEffect } from 'react'
import { useLocation, useNavigate } from 'react-router-dom'
//
import useGetUserInfo from './useGetUserInfo'
import { ROUTE_PATH, isLoginOrRegister, isNoNeedUserInfo } from '@/router'

function useNavPage(waitingUserData:boolean){
    const { username }=useGetUserInfo()
    const { pathname}=useLocation()
    const nav =useNavigate()

    useEffect(()=>{
        if(waitingUserData)
            return
        // already login
        if(username){
            if(isLoginOrRegister(pathname)){
                nav(ROUTE_PATH.QUESTION_EDIT)
            }
            return
        }
        // not login
        if(isNoNeedUserInfo(pathname)){
            return
        }else{
            nav(ROUTE_PATH.LOGIN)
        }
    },[waitingUserData,username,pathname])
}

export default useNavPage